let axios = require("axios");
let fs = require("fs");
const proxy = {
    host:"127.0.0.1",
    port: "7891",
    protocol: "http"
};
let headers = {
    accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "accept-encoding": "gzip, deflate, br",
    "accept-language": "zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7",
    cookie: "ipb_session_id=41682ef2211d381699a722378986d6e4; ipb_member_id=6722035; ipb_pass_hash=6f77e4c8018f782230ec66dc1c3f671c; sk=3m0dkv5h5rlg3d57riigckfimvwc; nw=1; event=1661322056",
    referer: "https://e-hentai.org/",
    "sec-fetch-dest": "document",
    "sec-fetch-mode": "navigate",
    "sec-fetch-site": "none",
    "sec-fetch-user": "?1",
    "upgrade-insecure-requests": "1",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36"
}



;
(async() => {
    // https://e-hentai.org/g/1975084/3e453d328a/
    // genshin
    // https://e-hentai.org/g/2278780/2d3676a211/
    // https://e-hentai.org/g/2262994/0f1eabe614/
    // exhibitionism 66
    
    // 
    await dowload({url:"https://e-hentai.org/g/2262994/0f1eabe614/"})
    await dowload({url:"https://e-hentai.org/g/2278780/2d3676a211/"})
    await dowload({url:"https://e-hentai.org/g/1975084/3e453d328a/"})

    // exhibitionism    てるてるがーる
})();


async function sleep(time) {
    return new Promise((a) => {
        setTimeout(() => {
            a()
        }, time);
    })
}


async function dowload(props) {
    if(!props.p){props.p = 0}
    if(!props.i){props.i = 0}
    let {url, p} = props
    let name = url.split("/").filter(v => v && v!="").pop();
    if(!props.purls){
        props.purls = []
        try {
            fs.accessSync("./logs/"+name + "/purls")
            props.purls = fs.readFileSync("./logs/"+name + "/purls").toString().split("\n").filter(v => v && v!="")
        } catch (error) {
            
        }
    }
    if(!fs.existsSync("./logs/"+name)){
        fs.mkdirSync("./logs/"+name)
    }
    if(p == 0){
        let info = "url: " + url
        fs.writeFileSync("./logs/"+name + "/info", info)
    }
    let ret = await axios.get(encodeURI(url+(p?`?p=${p}`:"")), {
        headers, proxy
    })
    props.p++
    if(ret?.data) {
        let matchs = ret.data.match(/<a href="https:\/\/e-hentai.org\/s\/[^/]+\/[^/]+">/ig);
        if(matchs && matchs.length) {
            for (let index = 0; index < matchs.length; index++) {
                props.i++
                let s = matchs[index];
                let purl = /<a href="([^"]+)">/ig.exec(s)[1]
                if(props.purls.includes(purl)){
                    continue
                }
                // 请求下载图片到 "./logs/"+name + "/"
                try {
                    let phtml = await axios.get(purl, {
                        headers, proxy
                    })
                    if(phtml && phtml.data) {
                        // id="img" src="https://ytgoipd.vfaljepxpbtt.hath.network/h/a56cc64ebd2296a5785abb9b3e083b8cc3c0a1be-387095-1280-1808-jpg/keystamp=1661309100-be63b23933;fileindex=91194644;xres=1280/001.jpg"
                        if(/id="img"\s+src="([^"]+)"/.test(phtml.data)){
                            let imgurl = RegExp.$1;
                            let imgname = imgurl.split("/").filter(v => v && v!= "").pop()
                            let imgPath = "./logs/"+name + "/" + props.i+"_"+imgname
                            let existImg = false;
                            try {
                                fs.accessSync(imgPath)
                                existImg = true
                            } catch (e) {
                                
                            }
                            if(!existImg) {
                                await sleep(2000 + 1000 * Math.random())
                                console.log("fetch img: " + imgurl);
                                let imgdata = await axios.get(imgurl, {
                                    responseType: 'arraybuffer',proxy,headers,
                                });
                                if (imgdata&&imgdata.data) {
                                    if(imgdata.data.length == 0) {
                                        console.log(imgurl + "下载失败");
                                    } else {
                                        fs.writeFileSync(imgPath, imgdata.data, 'binary');
                                        fs.appendFileSync("./logs/"+name + "/purls", purl+"\n")
                                    }
                                } else {
                                    console.log("purl err: " + purl);
                                    fs.appendFileSync("./logs/"+name + "/err", purl+"\n")
                                }
                            } else {
                                await sleep(1000)
                            }
                        } else {
                            console.log("purl err: " + purl);
                            fs.appendFileSync("./logs/"+name + "/err", purl+"\n")
                        }
                    }
                } catch (error) {
                    console.log("purl err: " + purl);
                    fs.appendFileSync("./logs/"+name + "/err", purl+"\n")
                }

            }

            if(matchs.length >= 40) {
                await dowload(props)
            }
            
        } else {
            console.log("未匹配到");
            fs.writeFileSync("./serch.html", ret.data)
        }
    } else {
        console.log("url 请求失败:" + url+(p?`?p=${p}`:""));
    }
}
